package resultPackage;

import java.io.ByteArrayInputStream;

import org.tmatesoft.svn.core.SVNCommitInfo;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.auth.BasicAuthenticationManager;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.io.ISVNEditor;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.io.diff.SVNDeltaGenerator;

public class Connector {
	SVNURL url;
	SVNRepository repository;
    ISVNEditor editor = null;

	//TODO - Convert into an interface
	public Connector(){
			System.out.println("New Connector");
	}

	public void login(String username, String password, String url) throws SVNException {		
		DAVRepositoryFactory.setup();
		System.out.println("Repository set");
		this.url = SVNURL.parseURIEncoded(url);

		// implement the authentication manager interface with username password
		// see http://wiki.svnkit.com/Authentication
		ISVNAuthenticationManager authManager = new BasicAuthenticationManager(username,password);
		System.out.println("Authentication completed");
        
		//Create repository driver to bind to defined repository location
		System.out.println("URL "+url);

		repository = SVNRepositoryFactory.create( this.url);
        
        //Set an auth manager which will provide user credentials
        repository.setAuthenticationManager(authManager);  

        System.out.println("After setting auth manager");
		
	}

	// URL
	public SVNURL getUrl() {
		return url;
	}
	public void setUrl(String url) {
		 try {
			this.url = SVNURL.parseURIEncoded(url);
		} catch (SVNException e) {
			e.printStackTrace();
		}

	}
	
	public void checkOut() throws SVNException{
		
		System.out.println("Start checkOut()");

		
        long latestRepoRev = 0;
        
		try {
			latestRepoRev = repository.getLatestRevision();
		} catch (SVNException e) {

			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
        System.out.println("latest revision is "+latestRepoRev);            
        
        // Define comment for checkout
        String logMessage = "Creating File";
        
        // Set editor
        System.out.println("setting Editor");
		try {
			editor = repository.
					getCommitEditor( logMessage , null /*locks*/ , true /*keepLocks*/ , new CommitMediator() /*mediator*/ );
		} catch (SVNException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        // Open version of directory            
        System.out.println("Open root");
        try {
			editor.openRoot(latestRepoRev);
		} catch (SVNException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        System.out.println("End checkOut()");
	}
	
	public void update(){
		System.out.println("Start update()");
        
        // Define filename
        String filename = "src/Test.txt";
        
        // Add new file
        try {
			editor.addFile( filename , null , -1 );
		} catch (SVNException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
        
/*        // Update existing file
        System.out.println("Updating file");            
        try {
			editor.openFile( filename , latestRepoRev );
		} catch (SVNException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
*/        
        // Start to apply the text delta of file
        System.out.println("Check Sum");
        try {
			editor.applyTextDelta(filename, null);
		} catch (SVNException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        byte[] newData = "Testing Adding File'.\n".getBytes();
        
        // send delta to editor
        SVNDeltaGenerator deltaGenerator = new SVNDeltaGenerator();
        String checksum = null;
		try {
			checksum = deltaGenerator.
					sendDelta(filename, new ByteArrayInputStream(newData), editor, true);
		} catch (SVNException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        System.out.println("close file");
        try {
			editor.closeFile(filename, checksum);
		} catch (SVNException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

        System.out.println("close dir");
        try {
			editor.closeDir();
		} catch (SVNException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        System.out.println("End update()");
	}
	
	public void commit(){
        System.out.println("Enter commit()");
        // Commit changes
        SVNCommitInfo info;
		try {
			info = editor.closeEdit();
	        System.out.println(info.getErrorMessage());
	        
		} catch (SVNException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("Exit commit()");
	}
	
}
